/**
 * Lukasz Muziol
 *
 * MEng Individual Project
 * Gride: A teaching oriented Groovy IDE  *
 *
 */

package gride;

import gride.events.EventManager;
import gride.gui.GUIManager;
import gride.gui.GrideSplashScreen;
import gride.plugin.PluginManager;
import gride.project.ProjectManager;
import java.awt.EventQueue;
import java.io.File;
import javax.swing.UIManager;

/**
 * This class contains the main method, which starts the program.
 *
 * @author Lukasz Muziol
 */
public class GrideMain {

    /**
     * Entry point to the program. Validates that the required environment
     * variables are set and starts the program.
     *
     * @param args Command-line arguments
     */
    public static void main(String args[])
    {
        // get the value of environment variable JAVA_HOME
        GrideSettings.JAVA_HOME_DIR = System.getenv("JAVA_HOME");
        GrideSettings.GROOVY_HOME_DIR = System.getenv("GROOVY_HOME");

        // obtain the locations of JAVA_HOME and GROOVY_HOME
        if (!checkJavaHomeDir(GrideSettings.JAVA_HOME_DIR) || 
            !checkGroovyHomeDir(GrideSettings.GROOVY_HOME_DIR))
            System.exit(0);        

        // set the look and feel of the application
        try
        {            
            // change to Nimbus look and feel
            for (UIManager.LookAndFeelInfo info : 
                 UIManager.getInstalledLookAndFeels())
                if (info.getName().equals("Nimbus"))
                    UIManager.setLookAndFeel(info.getClassName());
        }
        catch (Exception e)
        {
            // look and feel not found, go with the default
        }

        // display Gride splash screen
        final GrideSplashScreen splash = new GrideSplashScreen();
        splash.setVisible(true);

        // instantiate core components and run the GUI
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                // create an event manager
                EventManager eventManager = new EventManager();

                // create a GUI manager
                GUIManager guiManager = new GUIManager(eventManager);
                // create a project manager
                ProjectManager projectManager = new ProjectManager(eventManager,
                                                                   guiManager);

                // create a plugin manager
                PluginManager pluginManager =
                    new PluginManager(eventManager, projectManager, guiManager);

                // all loaded - display the GUI
                guiManager.pack();
                guiManager.setVisible(true);
                
                // close splash screen 
                splash.setVisible(false);
            }
        });
    }


    /**
     * Validates the value of JAVA_HOME environment variable
     *
     * @param dir Value of JAVA_HOME environment variable
     * @return true if given location valid, false otherwise
     */
    private static boolean checkJavaHomeDir(String dir)
    {
        // make sure the directory is set
        if (dir == null || dir.equals(""))
        {
            System.out.println("Error: JAVA_HOME environment variable not set");
            return false;
        }

        // check if the location contains java binary
        if (!(new File(dir + "/bin/java").exists()))
        {
            System.out.println("Error: couldn't locate the java binary. " +
                               "Make sure JAVA_HOME environment variable is" +
                               " correct.");
            return false;
        }

        return true;
    }


    /**
     * Validates the value of GROOVY_HOME environment variable
     *
     * @param dir Value of GROOVY_HOME environment variable
     * @return true if given location valid, false otherwise
     */
    private static boolean checkGroovyHomeDir(String dir)
    {
        // make sure GROOVY_HOME is set
        if (dir == null || dir.equals(""))
        {
            System.out.println("Error: GROOVY_HOME environment variable not " +
                               "set. Please set it or provide its value as " +
                               "a command line argument.");
            return false;
        }

        // check if the location contains groovyc
        if (!(new File(dir + "/bin/groovyc")).exists())
        {
            System.out.println("Error: couldn't locate the groovyc binary. " +
                               "Make sure GROOVY_HOME environment variable is" +
                               " correct. Alternatively, you can provide its" +
                               "value as a command line argument.");
            return false;
        }

        // check if the location contains groovysh
        if (!(new File(dir + "/bin/groovysh")).exists())
        {
            System.out.println("Error: couldn't locate the groovysh binary. " +
                               "Make sure GROOVY_HOME environment variable is" +
                               " correct. Alternatively, you can provide its " +
                               "value as a command line argument.");
            return false;
        }

        return true;
    }
}
